package com.campsys.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campsys.domain.LoginUser;
import com.campsys.domain.TSysUser;
import com.campsys.domain.param.PageParam;
import com.campsys.domain.param.UserAddParam;
import com.campsys.domain.vo.AdminPageVo;
import com.campsys.domain.vo.UserInfoVo;
import com.campsys.mapper.UserMapper;
import com.campsys.mapping.UserMapping;
import com.campsys.service.IUserService;
import com.campsys.util.JWTUtil;
import com.campsys.util.RedisUtil;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, TSysUser> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public String createToken(String username, String password) {

        UsernamePasswordAuthenticationToken authenticationToken
                = new UsernamePasswordAuthenticationToken(username,password);
        //调用UserDetailServiceImpl中的loadUserByUsername方法
        Authentication authenticate = authenticationManager.authenticate(authenticationToken);
        if(authenticate == null){
            throw new RuntimeException("认证失败");
        }

        //getPrincipal()返回登录对象
        LoginUser loginUser = (LoginUser) authenticate.getPrincipal();

        String token = JWTUtil.createToken(loginUser.getTSysUser());

        //缓存用户信息
        redisUtil.setCacheObject("user:"+loginUser.getTSysUser().getId(),loginUser);

        return token;
    }

    @Override
    public List<AdminPageVo> getAdminPage(PageParam pageParam) {
        Integer offset = (pageParam.getPageNum()- 1) * pageParam.getPageSize() ;
        Integer limit = pageParam.getPageSize();
        return userMapper.getAdminPage(offset,limit);
    }

    @Override
    public Long getAdminPageTotal() {
        return userMapper.getAdminPageTotal();
    }

    @Override
    public UserInfoVo getUserInfoByUserId(Long userId) {
        TSysUser user = userMapper.selectById(userId);
        UserInfoVo userInfoVo = UserMapping.INSTANCE.to(user);

        userInfoVo.setRoleId(userMapper.getRoleIdByUserId(userId));
        userInfoVo.setPermissions(userMapper.getPermissionsByUserId(userId));
        return userInfoVo;
    }

    @Transactional
    @Override
    public Boolean addAdmin(UserAddParam userAddParam) {

        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode("123456");

        TSysUser user = UserMapping.INSTANCE.to(userAddParam);
        user.setPassword(encode);
        user.setStatus("1");
        int rs1 = userMapper.insert(user);

        boolean rs2 = userMapper.addRoleIdByUserId(user.getId(),2l);

        return rs1 == 1 & rs2 ? true : false ;
    }
}
